/**
 * Author: zhezhen.cheng
 * Creation Time: 2016/6/14.
 * Description:...
 */
define(['jquery', 'artTemplate' , '../m/dhcpPostM' , '../../../common/js/lib/text!../../../tpl/footer.tpl'],
    function ($, artTemplate , dhcpPostM , footerT) {
        function dhcpV(datas) {
            var checkVal = (datas.data.dhcpEnabled != null ) ? datas.data.dhcpEnabled : 0;
            var ischeck = (datas.data.dhcpEnabled != null && datas.data.dhcpEnabled !=0) ? 'check-span-current' : '';
            var dhcpStart = (datas.data.dhcpStart != null ) ? datas.data.dhcpStart : '';
            var dhcpEnd = (datas.data.dhcpEnd != null ) ? datas.data.dhcpEnd : '';
            var dhcpPriDns = (datas.data.dhcpPriDns != null ) ? datas.data.dhcpPriDns : '';
            var dhcpSecDns = (datas.data.dhcpSecDns != null ) ? datas.data.dhcpSecDns : '';
            var dhcpLease = '';
            var dhcpLease_text = '';
            if(datas.data.dhcpLease == 0){
                dhcpLease_text= '30分钟';
            }else if(datas.data.dhcpLease == 1){
                dhcpLease_text= '1小时';
            }else if(datas.data.dhcpLease == 6){
                dhcpLease_text= '6小时';
            }else if(datas.data.dhcpLease == 12){
                dhcpLease_text= '12小时';
            }else if(datas.data.dhcpLease == 24){
                dhcpLease_text= '1天';
            }else if(datas.data.dhcpLease == 72){
                dhcpLease_text= '3天';
            }else if(datas.data.dhcpLease == 168){
                dhcpLease_text= '7天';
            }
            var html = '';
            html+='<div class="header"><a href="/h5-wizard/index.html#system-state" class="back-a"></a><p>DHCP服务</p><a class="header-right">保存</a></div>';
            html+='<div class="content">';
            html+='<div class="success-txt m-t-6"><i>DHCP服务</i><span class="check-span '+ischeck+'"><input type="hidden" value="'+checkVal+'" class="wifi_enable"></span></div>';
            html+='<ul class="device-txt m-t-6">';
            html+='<li class="ok-mes"><i class="size-color">IP地址池</i></li>';
            html+='<li class="ok-mes right-input"><i>起始IP</i><input type="text" class="dhcpStart" value="'+dhcpStart+'"></li>';
            html+='<li class="ok-mes right-input"><i>结束ID</i><input type="text" class="dhcpEnd" value="'+dhcpEnd+'"></li>';
            html+='<li class="ok-mes ul-li-select"><i>结束ID</i>';
            html+='<div class="selects"><input type="text" class="link-mode out_date" readonly="readonly" placeholder="请选择过期时间" value="'+dhcpLease_text+'" vals="'+datas.data.dhcpLease+'"><span class=""></span>';
            html+='<ul class="name-list" style="display: none;"><li value="0" >30分钟</li><li value="1" >1小时</li><li value="6" >6小时</li><li value="12" >12小时</li><li value="24" >1天</li><li value="72" >3天</li><li value="168" >7小时</li></ul></div></li>';
            html+='</ul>';
            html+='<ul class="device-txt m-t-6">';
            html+='<li class="ok-mes"><i class="size-color">DNS设置</i></li>';
            html+='<li class="ok-mes right-input"><i>首选DNS</i><input type="text" class="dhcpPriDns" value="'+dhcpPriDns+'"></span></li>';
            html+='<li class="ok-mes right-input"><i>备用DNS</i><input type="text" class="dhcpSecDns" value="'+dhcpSecDns+'"></span></li>';
            html+='</ul>';
            html+='</div>';

            var footer = artTemplate.compile(footerT);
            html += footer({type:4});
            $(".webApp-content").html(html);

            $(".m-t-6").on('click','.check-span',function(){
                var input_obj = $(this).find('input');
                if($(this).hasClass('check-span-current')){
                    $(this).removeClass('check-span-current');
                    input_obj.val('0');
                }else{
                    $(this).addClass('check-span-current');
                    input_obj.val('1');
                }
            });
            $(".webApp-content").on('click','.selects',function(event){
                var select_obj = $(this).find('.name-list');
                $(".select").find('.name-list').hide();
                if(select_obj.css('display') == 'none'){
                    select_obj.show();
                }
                event.stopPropagation();
            });
            $(".webApp-content").on('click','.name-list li',function(event){
                var vals = $(this).attr('value');
                var text_val = $(this).text();
                $(this).closest('.selects').find('input').val(text_val);
                $(this).closest('.selects').find('input').attr('vals',vals);
                $(this).parent().hide();
            });
            $(document).click(function(event){
                $(".select").find('.name-list').hide();
            });
            $(".header-right").on('click',function(){
                var wifi_enable = parseInt($(".wifi_enable").val());
                var dhcpStarts = $(".dhcpStart").val();
                var dhcpEnds = $(".dhcpEnd").val();
                var dhcpPriDnsEr = $(".dhcpPriDns").val();
                var dhcpSecDnsEr = $(".dhcpSecDns").val();
                var dhcpLeases = parseInt($(".out_date").attr('vals'));
                var data = JSON.stringify({
                    "dhcpEnabled": wifi_enable,
                    "dhcpStart": dhcpStarts,
                    "dhcpEnd": dhcpEnds,
                    "dhcpPriDns": dhcpPriDnsEr,
                    "dhcpSecDns": dhcpSecDnsEr,
                    "dhcpLease": dhcpLeases,
                })
                dhcpPostM(data);
            });
        }

        return dhcpV;
    });